import os
import django
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'AnsibleGUI.settings')
django.setup()
from common.AnsibleExec import AnsibleExec

resource={"group":{"hosts":
                     [{"hostid":"123","hostname":"192.168.2.150","hostip":"192.168.2.150","username":"root","password":"123456","k1":"v1"}],
        "groupvars":{
  "nginx_stream_template_enable": True,
  "nginx_stream_template": {
    "default": {
      "template_file": "stream/default.conf.j2",
      "conf_file_name": "default.conf",
      "conf_file_location": "/etc/nginx/conf.d/stream/",
      "network_streams": {
        "default": {
          "listen": {
            "listen_localhost": {
              "ip": "0.0.0.0",
              "port": 80,
              "ssl": False,
              "opts": [

              ]
            }
          },
          "ssl": {
            "cert": "/etc/ssl/certs/default.crt",
            "key": "/etc/ssl/private/default.key",
            "dhparam": "/etc/ssl/private/dh_param.pem",
            "protocols": "TLSv1 TLSv1.1 TLSv1.2",
            "ciphers": "HIGH:!aNULL:!MD5",
            "prefer_server_ciphers": True,
            "session_cache": "none",
            "session_timeout": "5m",
            "disable_session_tickets": False,
            "trusted_cert": "/etc/ssl/certs/root_CA_cert_plus_intermediates.crt",
            "ecdh_curve": "auto"
          },
          "include_files": [

          ],
          "proxy_pass": "backend",
          "proxy_timeout": "3s",
          "proxy_connect_timeout": "1s",
          "proxy_protocol": False,
          "proxy_ssl": {
            "cert": "/etc/ssl/certs/proxy_default.crt",
            "key": "/etc/ssl/private/proxy_default.key",
            "trusted_cert": "/etc/ssl/certs/proxy_ca.crt",
            "protocols": "TLSv1 TLSv1.1 TLSv1.2",
            "ciphers": "HIGH:!aNULL:!MD5",
            "verify": False,
            "verify_depth": 1,
            "session_reuse": True
          },
          "health_check_plus": False
        }
      },
      "upstreams": {
        "upstream1": {
          "name": "backend",
          "lb_method": "least_conn",
          "zone_name": "backend",
          "zone_size": "64k",
          "sticky_cookie": False,
          "servers": {
            "server1": {
              "address": "localhost",
              "port": 8080,
              "weight": 1,
              "health_check": "max_fails=1 fail_timeout=10s"
            }
          }
        }
      }
    }
  }
}
    }
  }


def RunAPI():
    task_list = [
      dict(action=dict(module='shell', args='ls')),
      # dict(action=dict(module='debug', args=dict(msg='{{ goodjob.name }}'))),
    ]
    runner = AnsibleExec(resource)
    runner.runansible(host_list=['bbc'],task_list=task_list)
    # result = runner.results_raw
    # print(result)

def RunPlaybook():
    runner = AnsibleExec(resource)
    runner.run_playbook(playbookpath='/etc/ansible/Nginx_Group02.yaml')


if __name__ == '__main__':
    RunPlaybook()



    # RunPlaybook()
